package com.sharemarking.system.service.impl;

import com.sharemarking.system.dto.UserDetailImpl;
import com.sharemarking.system.mapper.PermissionMapper;
import com.sharemarking.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {
	
	@Autowired
	private PermissionMapper pm;

	@Override
	public boolean hasPermission(Integer permissionId) {
		UserDetailImpl udi=(UserDetailImpl)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		return udi.getPermissions().contains(permissionId);
	}

	@Override
	public boolean hasPermission(String domain, String permission) {
		Integer pId=pm.getIdByDomainAndName(domain, permission);
		return hasPermission(pId);
	}

}
